package com.tuju.jetpackfirstdemo

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.DisplayMetrics
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.baize.fireeyekotlin.utils.log.L
import com.tuju.jetpackfirstdemo.base.BaseActivity
import com.tuju.jetpackfirstdemo.ui.SearchKeyActivity
import com.tuju.jetpackfirstdemo.ui.img_down.ImgDownActivity
import com.tuju.jetpackfirstdemo.ui.WeatherHomeActivity
import com.tuju.jetpackfirstdemo.ui.animator.AnimatorActivity
import com.tuju.jetpackfirstdemo.ui.auto_sizeing.AutoSizeingActivity
import com.tuju.jetpackfirstdemo.ui.bugly.BuglyActivity
import com.tuju.jetpackfirstdemo.ui.bugly.ExceptionActivity
import com.tuju.jetpackfirstdemo.ui.circle_view.CircleActivity
import com.tuju.jetpackfirstdemo.ui.complexform.ComplexFormActivity
import com.tuju.jetpackfirstdemo.ui.constraint_layout.ConstaintLayoutActivity
import com.tuju.jetpackfirstdemo.ui.custom_view.CustomViewActivity
import com.tuju.jetpackfirstdemo.ui.flash.FlashActivity
import com.tuju.jetpackfirstdemo.ui.flexbox.FlexBoxActivity
import com.tuju.jetpackfirstdemo.ui.k.KLineActivity
import com.tuju.jetpackfirstdemo.ui.link.LinkActivity
import com.tuju.jetpackfirstdemo.ui.list.ListActivity
import com.tuju.jetpackfirstdemo.ui.log.LogActivity
import com.tuju.jetpackfirstdemo.ui.longpic.LongPicActivity
import com.tuju.jetpackfirstdemo.ui.main.MainFragment
import com.tuju.jetpackfirstdemo.ui.mpchart.MPAndroidChartActivity
import com.tuju.jetpackfirstdemo.ui.opengl.OpenGLActivity
import com.tuju.jetpackfirstdemo.ui.opengl2.OpenGLES20Activity
import com.tuju.jetpackfirstdemo.ui.processes.RunningProcessActivity
import com.tuju.jetpackfirstdemo.ui.recordings.RecordingActivity
import com.tuju.jetpackfirstdemo.ui.room_demo.RoomDemoActivity
import com.tuju.jetpackfirstdemo.ui.rv_demo.RvDemoActivity
import com.tuju.jetpackfirstdemo.ui.rv_demo2.ScheduleDemoActivity
import com.tuju.jetpackfirstdemo.ui.rv_loadmore.LoadMoreActivity
import com.tuju.jetpackfirstdemo.ui.scroll.ScrollActivity
import com.tuju.jetpackfirstdemo.ui.tablayout.TabLayoutActivity
import com.tuju.jetpackfirstdemo.ui.textbanner.TextBannerActivity
import com.tuju.jetpackfirstdemo.ui.textrich.TextRichActivity
import com.tuju.jetpackfirstdemo.ui.texttospeech.SpeechActivity
import com.tuju.jetpackfirstdemo.ui.timer.TimerSelectActivity
import com.tuju.jetpackfirstdemo.ui.video.VideoActivity
import com.tuju.jetpackfirstdemo.ui.wordcloud.WordCloudActivity
import com.tuju.jetpackfirstdemo.utils.*
import kotlinx.android.synthetic.main.main_activity.*
import java.util.*
import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReentrantLock

class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        App.addActivity(this@MainActivity)
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                    .replace(R.id.container, MainFragment.newInstance())
                    .commitNow()
        }
        initView()
    }

    private fun initView() {
        btn_toast.setOnClickListener {
            Toast.makeText(App.instants, "哈哈哈111111111111111...", Toast.LENGTH_SHORT).show()
            DebugUtil.printLogToSDCard(this@MainActivity, "哈哈哈。。。。。")
        }

        btn_lin.setOnClickListener {
            val lock = ReentrantLock()
            L.i(msg = check(lock, { "我是lambda方法体" }))
            Toast.makeText(App.instants, "哈哈哈2222...", Toast.LENGTH_SHORT).show()
            Thread(Runnable {
                DebugUtil.printLogToSDCard(this@MainActivity, "Thread哈哈哈2222。。。。。")
            }).start()
        }

        btn_weather.setOnClickListener {
            startActivity(Intent(this@MainActivity, WeatherHomeActivity::class.java))
        }

        btn_rv.setOnClickListener {
            startActivity(Intent(this@MainActivity, RvDemoActivity::class.java))
        }

        btn_room.setOnClickListener {
            startActivity(Intent(this@MainActivity, RoomDemoActivity::class.java))
        }

        btn_rv2.setOnClickListener {
            startActivity(Intent(this@MainActivity, ScheduleDemoActivity::class.java))
        }

        btn_flex.setOnClickListener {
            startActivity(Intent(this@MainActivity, FlexBoxActivity::class.java))
        }

        btn_custom.setOnClickListener {
            startActivity(Intent(this@MainActivity, CustomViewActivity::class.java))
        }

        btn_loadmore.setOnClickListener {
            startActivity(Intent(this@MainActivity, LoadMoreActivity::class.java))
        }

        btn_circle.setOnClickListener {
            startActivity(Intent(this@MainActivity, CircleActivity::class.java))
        }

        btn_video.setOnClickListener {
            startActivity(Intent(this@MainActivity, VideoActivity::class.java))
        }

        btn_recordings.setOnClickListener {
            startActivity(Intent(this@MainActivity, RecordingActivity::class.java))
        }

        btn_scroll.setOnClickListener {
            startActivity(Intent(this@MainActivity, ScrollActivity::class.java))
        }

        btn_exception.setOnClickListener {
            startActivity(Intent(this@MainActivity, ExceptionActivity::class.java))
        }

        val randState = arrayListOf<String>("空", "约", "锁")
        btn_getRandom.setOnClickListener {
            showToast("${getRandomNum(0, randState.size - 1)}")
        }

        btn_getDensity.setOnClickListener {
            // 通过WindowManager获取
            var dm = DisplayMetrics()
            getWindowManager().getDefaultDisplay().getMetrics(dm)
            System.out.println("width-display :" + dm.widthPixels)
            System.out.println("heigth-display :" + dm.heightPixels)
            System.out.println("density :" + dm.density)
            System.out.println("densityDpi :" + dm.densityDpi)
//            showToast("宽：${dm.widthPixels} 高：${dm.heightPixels}")
            showToast(DisplayUtil.printDisplayMetricsInfo(this))
//            DisplayUtil.printDisplayMetrics(this)
        }

        btn_opengl.setOnClickListener {
            startActivity(Intent(this@MainActivity, OpenGLActivity::class.java))
        }

        btn_animator.setOnClickListener {
            startActivity(Intent(this@MainActivity, AnimatorActivity::class.java))
        }

        btn_constainlayout.setOnClickListener {
            startActivity(Intent(this@MainActivity, ConstaintLayoutActivity::class.java))
        }
        btn_longpic.setOnClickListener {
            startActivity(Intent(this@MainActivity, LongPicActivity::class.java))
        }

        btn_getMac.setOnClickListener {
            val macStr = BaiZeUtil.getMac(App.instants)
            BaseTools.copy(macStr)
            showToast("已复制:$macStr")
        }

        btn_antosizeing.setOnClickListener {
            startActivity(Intent(this@MainActivity, AutoSizeingActivity::class.java))
        }

        btn_bugly.setOnClickListener {
            startActivity(Intent(this@MainActivity, BuglyActivity::class.java))
        }

        btn_complex_form.setOnClickListener {
            startActivity(Intent(this@MainActivity, ComplexFormActivity::class.java))
        }

        btn_text_banner.setOnClickListener {
            startActivity(Intent(this@MainActivity, TextBannerActivity::class.java))
        }

        btn_k_xian.setOnClickListener {
            startActivity(Intent(this@MainActivity, KLineActivity::class.java))
        }

        btn_richtext.setOnClickListener {
            startActivity(Intent(this@MainActivity, TextRichActivity::class.java))
        }

        btn_timer.setOnClickListener {
            startActivity(Intent(this@MainActivity, TimerSelectActivity::class.java))
        }

        btn_mpchart.setOnClickListener {
            startActivity(Intent(this@MainActivity, MPAndroidChartActivity::class.java))
        }

        btn_speech.setOnClickListener {
            startActivity(Intent(this@MainActivity, SpeechActivity::class.java))
        }

        btn_img_down.setOnClickListener {
            startActivity(Intent(this@MainActivity, ImgDownActivity::class.java))
        }

        btn_flash.setOnClickListener {
            startActivity(Intent(this@MainActivity, FlashActivity::class.java))
        }

        btn_word_cloud.setOnClickListener {
            startActivity(Intent(this@MainActivity, WordCloudActivity::class.java))
        }

        btn_link.setOnClickListener {
            startActivity(Intent(this@MainActivity, LinkActivity::class.java))
        }

        btn_opengl2.setOnClickListener {
            startActivity(Intent(this@MainActivity, OpenGLES20Activity::class.java))
        }

        btn_log.setOnClickListener {
            startActivity(Intent(this@MainActivity, LogActivity::class.java))
        }

        btn_qq.setOnClickListener {
            val packageName = "com.palmap.localverticalhospital"//要打开应用的包名,以微信为例
            BaseTools.lancherApp(this@MainActivity, packageName)
        }

        btn_restart.setOnClickListener {
            val intent = Intent(getApplicationContext(), MainActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            val restartIntent = PendingIntent.getActivity(
                    getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
            //退出程序
            val mgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, restartIntent) // 1秒钟后重启应用
            CrashHandler.exitAPP(applicationContext)
//            android.os.Process.killProcess(android.os.Process.myPid())
        }

        btn_search.setOnClickListener {
            SearchKeyActivity.start(this@MainActivity)
        }

        btn_list.setOnClickListener {
            startActivity(Intent(this@MainActivity, ListActivity::class.java))
        }

        btn_process.setOnClickListener {
            startActivity(Intent(this@MainActivity, RunningProcessActivity::class.java))
        }

        btn_tablayout.setOnClickListener {
            startActivity(Intent(this@MainActivity, TabLayoutActivity::class.java))
        }
    }

    //获取范围内随机数
    private fun getRandomNum(min: Int, max: Int): Int {
        val random = Random()
        return random.nextInt((max - min) + 1) + min
    }

    //内联函数
    inline fun <T> check(lock: Lock, body: () -> T): T {
        lock.lock()
        try {
            return body()
        } finally {
            lock.unlock()
        }
    }

    override fun onRestart() {
        super.onRestart()
        L.i(msg = "onRestart")
    }

    override fun onResume() {
        super.onResume()
        L.i(msg = "onResume")
    }

    override fun onStart() {
        super.onStart()
        L.i(msg = "onStart")
    }

    override fun onPause() {
        super.onPause()
        L.i(msg = "onPause")
    }

    override fun onStop() {
        super.onStop()
        L.i(msg = "onStop")
    }

    override fun onDestroy() {
        L.i(msg = "onDestroy")
//        showToast("ಠ_ಠ我有一个特异功能:1秒后重启!")
//        val intent = Intent(getApplicationContext(), MainActivity::class.java)
//        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
//        val restartIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
//        //退出程序
//        val mgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
//        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, restartIntent) // 1秒钟后重启应用
        super.onDestroy()
    }

}
